/*
  The MIT License
  
  Copyright (c) 2010 smath-view project
  
  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:
  
  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.
  
  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
*/
package pl.smath.renderer.model;

import gwt.g2d.client.graphics.Surface;
import gwt.g2d.client.graphics.canvas.Context;
import pl.smath.renderer.model.sign.RootSign;
import pl.smath.renderer.renderer.TermRendererException;
import pl.smath.renderer.utils.Cursor;
import pl.smath.renderer.utils.Rectangle;

/**
 * RootView - represents rendered root.
 * 
 * @author Damian Rodziewicz
 *
 */
public class RootView extends View {
	
	private RootSign rootSign;
	private View base;
	private View degree;
	private Double offset;
	private Double space;
	

	/**
	 * Constructor
	 */
	public RootView(View base, View degree) {
		super("$");
		this.setBase(base);
		this.setDegree(degree);
		this.rootSign = new RootSign();
	}
	
	/**
	 * render text at the current position
	 * @param canvas
	 */
	@Override
	public void render(Surface canvas) throws TermRendererException {
		if ((this.getFont() == null)||(this.getFontHeight() == null)) {
			throw new TermRendererException();
		}
		canvas.setFont(this.getFont());
		
		Cursor cursor = new Cursor(this.getCursor().getX(), this.getCursor().getY() - this.getBaselineOffset());
		
		Double minusWidth = this.minusSign.getWidth();
		
		if (this.isNegative()) this.minusSign.render(canvas, cursor);
		
		this.getBase().render(canvas);
		this.getDegree().render(canvas);
		
		if (this.isNegative()) 
			cursor.setX(cursor.getX() + minusWidth + this.getDegree().getWidth());
		else
			cursor.setX(cursor.getX() + this.getDegree().getWidth());

			
		this.rootSign.render(canvas, cursor);
		
		Double x = this.getBase().getX() - this.rootSign.getSpace();
		Double y = cursor.getY() - this.getHeight() + this.getSpace();// - this.getFontHeight() * 0.35d;
		Double length = this.getBase().getWidth() + this.getSpace();
		
		Context ctx = canvas.getContext();
		
		ctx.beginPath();
		ctx.moveTo(x, y);
		ctx.lineTo(x + length + this.rootSign.getSpace(), y);
		ctx.closePath();
		
		ctx.stroke();
	
		x = x + length + this.rootSign.getSpace();
		
		ctx.beginPath();
		ctx.moveTo(x, y);
		ctx.lineTo(x, y + this.getSpace()/2d);
		ctx.closePath();
		
		ctx.stroke();
	}
	

	/**
	 * set the position on canvas to render
	 * @param Cursor
	 */
	@Override
	public void setCursor(Cursor cursor) {
		super.setCursor(new Cursor(cursor.getX(), cursor.getY()));
			
		Surface canvas = new Surface();
		canvas.setFont(this.getFont());
			
		Double signWidth = this.rootSign.getWidth();
		Double minusWidth = this.minusSign.getWidth();
			
		Cursor toSet = new Cursor(cursor.getX(), cursor.getY() - this.getBaselineOffset());
		if (this.isNegative()) toSet.setX(toSet.getX() + minusWidth);
		
		this.getDegree().setCursor(new Cursor(toSet.getX(), toSet.getY() - offset));
		toSet.setX(toSet.getX() + this.getDegree().getWidth() + signWidth);
		this.getBase().setCursor(new Cursor(toSet.getX(), toSet.getY()));
	}
	
	/**
	 * @param font the font to set
	 */
	@Override
	public void setFont(String font) {
		if (font.indexOf("px")!=-1) {
			this.setFontHeight(new Double(font.substring(0, font.indexOf("px"))));
			this.font = font.substring(font.indexOf("px ")+3);
			this.minusSign.setFont(this.font, this.getFontHeight());
			this.rootSign.setFont(this.font, this.getFontHeight());

			this.setOffset(this.getFontHeight() * 0.6d);
			this.setSpace(this.getFontHeight() * 0.2d);
			
			this.getBase().setFont(font);
			
			if (this.getFontHeight() > 8) 
				this.getDegree().setFont((this.getFontHeight() - 4) + "px " + this.font);
			else
				this.getDegree().setFont(font);
			
			update();
		} else {
			this.setFontHeight(null);
			this.font = null;
		}
	}
	
	/**
	 * updates the view after changes
	 */
	@Override
	public void update() {
		Surface surface = new Surface();
		surface.setFont(this.getFont());
		
		Double signWidth = this.rootSign.getWidth();
		Double minusWidth = this.minusSign.getWidth();
		
		this.setRectangle(new Rectangle());
		this.setBaselineOffset(0d);
		
		this.setWidth(this.getBase().getWidth() + this.getDegree().getWidth() + signWidth + 2*this.getSpace());
		
		if(this.isNegative())
			this.setWidth(this.getWidth() + minusWidth);
		
		this.setHeight(Math.max(this.getBase().getHeight(), this.getDegree().getHeight() + this.offset) + this.getSpace());
		this.rootSign.setHeight(this.getHeight() - this.getSpace());
	}

	/**
	 * @param base the base to set
	 */
	public void setBase(View base) {
		this.base = base;
	}

	/**
	 * @return the base
	 */
	public View getBase() {
		return base;
	}

	/**
	 * @param degree the degree to set
	 */
	public void setDegree(View degree) {
		this.degree = degree;
	}

	/**
	 * @return the degree
	 */
	public View getDegree() {
		return degree;
	}

	/**
	 * @return the offset
	 */
	public Double getOffset() {
		return offset;
	}

	/**
	 * @param offset the offset to set
	 */
	public void setOffset(Double offset) {
		this.offset = offset;
	}

	/**
	 * @param space the space to set
	 */
	public void setSpace(Double space) {
		this.space = space;
	}

	/**
	 * @return the space
	 */
	public Double getSpace() {
		return space;
	}
}
